// pages/post-detail/post-detail.js
import { postList } from '../../data/data'

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    postList: {},
    _pid: null,
    collected: false,
    _postsCollected: {},
    showMsg: '',
    isPlaying: false,
    _mgr: '',
    _currentMusicId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //根据页面传输的文章id向data目录下data.js文件请求文章数据
    const postData = postList[options.pid];

    this.data._pid = options.pid;
    const postsCollected = wx.getStorageSync('posts_collected')
    if (postsCollected) {
      this.data._postsCollected = postsCollected
    }
    let collected = postsCollected[this.data._pid]
    if (collected === undefined) {
      collected = false
    }
    /**
     * 判断当前加载页面文章id是否与正在播放音乐的文章id相同，如果不相同则设置为未播放
     */
    if(options.pid != this.data._currentMusicId){
      app.gIsPlayingMusic = false
    }
    this.setData({
      'postList': postData,
      'collected': collected,
      isPlaying: app.gIsPlayingMusic
    });
    //wx.setStorageSync('flag', true);//设置缓存（同步）

    const mgr = wx.getBackgroundAudioManager()
    this.data._mgr = mgr
    mgr.onPlay(this.onMusicStart)
    mgr.onPause(this.onMusicStop)
    mgr.onEnded(this.onMusicOver)
  },

  onCollect: function (options) {

    /*
    wx.setStorageSync('collected', true)
    设计一个数据结构，储存文章id和收藏的状态
    {
      key:id,
      flag:true|false
    }
    {
      1:false 第一篇文章未被收藏
     }
    */

    let posts_collected = this.data._postsCollected
    posts_collected[this.data._pid] = !this.data.collected
    this.setData({
      collected: !this.data.collected
    })
    wx.setStorageSync('posts_collected', posts_collected)
    wx.showToast({
      title: this.data.collected ? '收藏成功' : '取消收藏',
      duration: 2000
    })
  },
  onShare: function () {
    wx.showActionSheet({
      itemList: [
        '分享到QQ',
        '分享到微信',
        '分享到朋友圈'
      ],
      success(res) {
        console.log(res);
      }
    })
  },
  onMusicStart: function (options) {
    const backgroundAudioManager = this.data._mgr;
    var music = postList[this.data._pid].music;
    backgroundAudioManager.src = music.url;
    backgroundAudioManager.title = music.title;
    backgroundAudioManager.coverImgUrl = music.coverImg;
    backgroundAudioManager.epname = '此时此刻'
    backgroundAudioManager.singer = '许巍'
    this.setData({
      isPlaying: true,
      _currentMusicId:this.data._pid
    })
    app.gIsPlayingMusic = true
  },
  onMusicStop: function (options) {
    const backgroundAudioManager = this.data._mgr;
    backgroundAudioManager.pause()
    this.setData({
      isPlaying: false
    })
    app.gIsPlayingMusic = false
  },
  onMusicOver:function(){
    const backgroundAudioManager = this.data._mgr;
    this.setData({
      isPlaying: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})